package com.yksj.healthtalk.comm;

import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 组列表
 * @author zhao
 *
 */
public class GroupListFragment extends Fragment implements OnItemClickListener {
	
	OnItemClickListener mItemClickListener;
	PullToRefreshListView mListView;
	public GroupListAdapter mAdapter;

	public void setItemClickListener(OnItemClickListener mClickListener){
		this.mItemClickListener = mClickListener;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mAdapter = new GroupListAdapter(getLayoutInflater(savedInstanceState));
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.group_list_layout,null);
		mListView = (PullToRefreshListView)view.findViewById(R.id.pull_refresh_listview);
		mListView.setOnItemClickListener(this);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mListView.setAdapter(mAdapter);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(mItemClickListener != null){
			mItemClickListener.onItemClick(parent,view, position, id);
		}
	}
	
	/**
	 * 
	 * 分组列表
	 * @author zhao
	 *
	 */
	public class GroupListAdapter extends BaseAdapter {
		JSONArray mArray = new JSONArray();
		LayoutInflater mInflater;
		private final ImageLoader mImageLoader;
		
		public GroupListAdapter(LayoutInflater inflater){
			this.mInflater = inflater;
			this.mImageLoader = ImageLoader.getInstance();
		}
		
		public void onDataChange(JSONArray jsonArray){
			this.mArray = jsonArray;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mArray.size();
		}
		
		@Override
		public boolean isEmpty() {
			return false;
		}

		@Override
		public JSONObject getItem(int position) {
			return mArray.getJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder viewHolder;
			final JSONObject object = getItem(position);
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.special_group_list_item,null);
				viewHolder.headerImageV = (ImageView)convertView.findViewById(R.id.head_image);
				viewHolder.nameTextV = (TextView)convertView.findViewById(R.id.name);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
			}
			object.getString("PATIENT_GROUP_ID");
			viewHolder.nameTextV.setText(object.getString("PATIENT_GROUP")+"("+object.getJSONArray("CUSTOMERIDS").size()+"人)");
			mImageLoader.displayImage("0",object.getString("client_Icon_background"),viewHolder.headerImageV);
			return convertView;
		}
	}
	
	private static class ViewHolder{
		ImageView headerImageV;
		TextView nameTextV;
	}
}
